import menus from "./data";
import store from "@/store"

// 根据路径得到面包屑的数据。
function getBreadcrumb(path:string) {    
    let arr = [{
        label:menus[0].title,
        to:menus[0].to
    }];

    for(let i=0;i<menus.length;i++){
        if(menus[i].to){
            if(menus[i].to === path){
                arr.push({
                    label:menus[i].title,
                    to:menus[i].to
                });
                return arr;
            }
        }else{
            for(let j=0;j<menus[i].children.length;j++){
                if(menus[i].children[j].to==path){
                     // 把父级的title先push
                    arr.push({
                        label:menus[i].title
                    });
                    arr.push({
                        label:menus[i].children[j].title,
                        to:menus[i].children[j].to
                    });
                    return arr;
                }
            }
        }
    }
}

export const doBreadcrumb=(path:string)=>{
    let arr = getBreadcrumb(path);
    store.commit({
        type:"changeBreadcrumbs",
        breadcrumbs:arr
    });
}